package com.izhangqian.signproject

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.izhangqian.signproject.activity.ProxyActivity
import com.izhangqian.signproject.adapter.HomePluginAdapter
import com.izhangqian.signproject.utils.LoadUtils
import com.izhangqian.signproject.utils.LogUtils
import com.izhangqian.signproject.utils.PluginManager
import com.izhangqian.signproject.viewmodel.PluginsViewModel
import com.izhangqian.signproject.webrequest.api.ControlGetRep

class MainActivity : AppCompatActivity() {

    lateinit var mPluginRv : RecyclerView
    lateinit var mPluginAdapter : HomePluginAdapter
    lateinit var mPluginViewModel : PluginsViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        LogUtils.e("WOO", "activity created")
        initData()
        initView()
        initEvent()
    }

    private fun initData() {
        mPluginViewModel = ViewModelProvider(this).get(PluginsViewModel::class.java)
        mPluginViewModel.getPlugins(this)
        mPluginViewModel.getConfig()
    }

    private fun initView() {
        mPluginRv = findViewById(R.id.main_actor_rv)
        mPluginAdapter = HomePluginAdapter(this)
        val manage = LinearLayoutManager(this)
        manage.orientation = LinearLayoutManager.VERTICAL
        mPluginRv.layoutManager = manage
        mPluginRv.adapter = mPluginAdapter
        val tv = findViewById<TextView>(R.id.tv_click)
        tv.setOnClickListener {
            val apkPath = LoadUtils.copyAssetAndWrite(this, "plugintest.apk")
            val apk = PluginManager.getInstance(this).getPlugin(apkPath)
            tv.setText(apk.stringa)
        }
        val tvstart = findViewById<TextView>(R.id.tv_start_activity)
        tvstart.setOnClickListener {
            val intent = Intent(this@MainActivity, ProxyActivity::class.java)
            intent.putExtra("className", "com.izhangqian.plugintest.PluginActivity")
            startActivity(intent)
        }
    }

    private fun initEvent() {
        mPluginViewModel.pluginBeans.observe(this,
            {
                mPluginAdapter.updateList(it)
            })
    }
}